cmake_minimum_required(VERSION 3.8.0)

message(${CMAKE_BUILD_TYPE})

if(${APPLE})
	message ("system MAC OS X detected")
endif ()

project(geoxfem VERSION 0.1.0)

# c++ version
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# destination
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin")

# Set a default build type if none was specified
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
	message("Setting build type to 'RelWithDebInfo' as none was specified.")
	set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build." FORCE)
	# Set the possible values of build type for cmake-gui
	set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
		"MinSizeRel" "RelWithDebInfo")
endif()

IF (CMAKE_BUILD_TYPE STREQUAL Release) #       用IF控制，可以实现Release版本不输出debug信息
	ADD_DEFINITIONS(-DNDEBUG)            #       不知道为什么DEBUG必须写成-DDEBUG，谁来解释一下吧
ENDIF()

# library type
set(LIB_TYPE OBJECT)

# make sure out-of-source builds
file(TO_CMAKE_PATH "${PROJECT_BINARY_DIR}/CMakeLists.txt" LOC_PATH)
message( "there is the CMakeLists.txt :" ${LOC_PATH})
if(EXISTS ".${LOC_PATH}")
	message(FATAL_ERROR "You cannot build in a source directory (or any directory with a CMakeLists.txt file). Please make a build subdirectory. Feel free to remove CMakeCache.txt and CMakeFiles.")
endif()
################################
# compiler flags

SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-DBOOST_LOG_DYN_LINK")
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
################################
# openmp

FIND_PACKAGE(OpenMP)
if(OPENMP_FOUND)
	ADD_DEFINITIONS(-DGEOXFEM_OPENMP)
	message("OPENMP FOUND")
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
	set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
################################
# boost

set(Boost_NO_BOOST_CMAKE ON)
find_package(Boost REQUIRED COMPONENTS log-mt log_setup-mt thread-mt date_time iostreams)
if(Boost_FOUND)
	message("there is the boost head file :" ${Boost_INCLUDE_DIRS})
	message("there is the boost library:" ${Boost_LIBRARIES})
	include_directories(${Boost_INCLUDE_DIRS})
else()
	message("Can't find the boost head file!")
endif()
################################

add_subdirectory(gmshParser)
add_subdirectory(src)
add_subdirectory(test)
################################

enable_testing()
add_test(NAME TEST_SHAPE COMMAND test_shp)
add_test(NAME TEST_TENSOR COMMAND test_tensor)
add_test(NAME TEST_MESH COMMAND test_mesh -- "../test/mesh_test.json")
add_test(NAME TEST_PLOT COMMAND test_plot -- "0")
add_test(NAME TEST_GAUSS COMMAND test_gauss)
add_test(NAME TEST_P52 COMMAND test_P52 -- "../test/test_P52/P52.json")
add_test(NAME TEST_CRACK COMMAND test_crack -- "../test/test_crack/crack.json")

